﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InsuranceCompany.Core.Domain.Objects
{
    public class History :Entity
    {
        private List<Event> Events;
        public List<Event> GetHistory() { return this.Events; }

        public History()
        {
            this.Events = new List<Event>();
        }
        public History(History h) :base(h)
        {
            this.Events = h.GetHistory();
        }
        public void AddEvent(Event ev) 
        {
            this.resetErrorMessage();
            if (ev.EventDate.CompareTo(DateTime.Today) == 1)
            {
                this.setErrorMessage("Can't enter future date!");
                return;
            }
            this.Events.Add(ev);
            SortHistoryByDate();
        }
        public Event GetEvent(DateTime date) { return this.Events.Find( x => x.EventDate == date ); }
        public Event GetEvent(int eventIndex) {return (Event)this.Events.ToArray().GetValue(eventIndex); }
        public void DeleteHistory() { this.Events.Clear(); }
        public void DeleteEvent(DateTime date) { this.Events.RemoveAll(x => x.EventDate == date); }
        public void DeleteEvent(int eventIndex) { this.Events.Remove( (Event)this.Events.ToArray().GetValue(eventIndex) ); }
        private void SortHistoryByDate() { this.Events.Sort(Event.compareEvents); }
    }
}
